package cn.kgc.easybuy.initalizer;

import cn.kgc.easybuy.service.ProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class ProductEsInitializer implements CommandLineRunner {
    private static final Logger logger = LoggerFactory.getLogger(ProductEsInitializer.class);

    @Autowired
    private ProductService productService;

    @Override
    public void run(String... args) throws Exception {
        logger.info("应用程序启动完成，开始初始化ES数据");
        try {
            productService.saveAllProductToEs();
            logger.info("商品ES数据初始化完成");
        } catch (Exception e) {
            logger.error("ES数据初始化失败", e);
        }
    }
}
